package com.dgut.finaldemo.controller;

import com.dgut.finaldemo.entity.Student;
import com.dgut.finaldemo.service.StudentService;
import com.dgut.finaldemo.util.R;
import com.dgut.finaldemo.vo.BarVO;
import com.dgut.finaldemo.vo.PieVO;
import com.dgut.finaldemo.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/student")
    @ResponseBody
    public DataVO student(Integer page, Integer limit){
        return studentService.findData(page,limit);
    }

    @GetMapping("/studentHtml")
    public String studentHtml(){
        return "student";
    }

    @RequestMapping("/addStudent")
    public String addStudent(){
        return "addStudent";
    }

    @RequestMapping("student/addStudentSubmit")
    @ResponseBody
    public R addStudentSubmit(Student student){
        if(student.getStatus()==null){
            student.setStatus(0);
        }
        studentService.addStudent(student);
        return R.ok();
    }

    @RequestMapping("student/updateStudentSubmit")
    @ResponseBody
    public R updateUserSubmit(Student student){
        if(student.getStatus()==null){
            student.setStatus(0);
        }else{
            student.setStatus(1);
        }
        studentService.updateStudent(student);
        return R.ok();
    }

    @RequestMapping("student/deleteStudentById")
    @ResponseBody
    public R deleteStudentById(Integer sid){
        studentService.deleteStudentById(sid);
        return R.ok();
    }

    @RequestMapping("student/searchStudent")
    @ResponseBody
    public DataVO searchStudent(Integer page, Integer limit,Student student){
        System.out.println(page);
        System.out.println(limit);
        System.out.println(student);
/*        System.out.println(sid);
        System.out.println(username);
        System.out.println(name);
        System.out.println(stdclass);
        System.out.println(phone);
        System.out.println(address);*/

        return studentService.searchStudent(page, limit,student);
    }

    @RequestMapping("/pieVO")
    @ResponseBody
    public List<PieVO> getBarVO(){
        return studentService.getPieClock();
    }

    @RequestMapping("/barVO")
    @ResponseBody
    public BarVO getBarVOList(){
        return studentService.getBarVO();
    }

    @GetMapping("/pieHtml")
    public String pieHtml(){
        return "pie";
    }

    @GetMapping("/barHtml")
    public String barHtml(){
        return "bar";
    }

}
